package com.bid.base.common.util.codec;


import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;

/**
 * <strong>Description : </strong> <br>
 * <p>
 * <strong>北京正源德标科技信息有限责任公司</strong> <br>
 * </p>
 * <br>
 *
 * @author : zhaots
 * @date Date : 2020年12月07日 18:22
 * 修改人 修改日期 修改描述<br>
 * -------------------------------------------<br>
 * <br>
 * <br>
 */
public class Des
{
  private static final String alg = "DES";
  private static final String transformation = "DES/ECB/PKCS5Padding";

  public static byte[] encode(byte[] src, String key)
    throws CodecException
  {
    return encode(src, key, "DES/ECB/PKCS5Padding");
  }

  public static byte[] encode(byte[] src, String key, String transformation)
    throws CodecException
  {
    SecureRandom sr;
    try
    {
      sr = new SecureRandom();

      DESKeySpec dks = new DESKeySpec(key.getBytes("UTF-8"));

      SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
      SecretKey securekey = keyFactory.generateSecret(dks);

      Cipher cipher = Cipher.getInstance(transformation);

      cipher.init(1, securekey, sr);

      return cipher.doFinal(src);
    }
    catch (Exception e) {
      throw new CodecException(e);
    }
  }

  public static byte[] decode(byte[] src, String key)
    throws CodecException
  {
    System.out.println("---------------------------" + key);
    return decode(src, key, "DES/ECB/PKCS5Padding");
  }

  public static byte[] decode(byte[] src, String key, String transformation)
    throws CodecException
  {
    SecureRandom sr;
    try
    {
      sr = new SecureRandom();

      DESKeySpec dks = new DESKeySpec(key.getBytes("UTF-8"));

      SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
      SecretKey securekey = keyFactory.generateSecret(dks);

      Cipher cipher = Cipher.getInstance(transformation);

      cipher.init(2, securekey, sr);

      return cipher.doFinal(src);
    }
    catch (Exception e) {
      e.printStackTrace();
      throw new CodecException(e);
    }
  }

  public static void main(String[] args)
    throws Exception
  {
    System.out.println(Base64.encode(encode("jdbc:sqlserver://192.168.240.246\\INST_SPADB;DatabaseName=FetionCore".getBytes(), "huodong12345")));

    System.out.println(Base64.encode(encode("jdbc:sqlserver://192.168.241.101\\INST_ACHVDB;DatabaseName=FetionVideo".getBytes(), "huodong12345")));
    System.out.println(Base64.encode(encode("FetionCore_Worker".getBytes(), "huodong12345")));

    System.out.println(Base64.encode(encode("FetionVideo".getBytes(), "huodong12345")));
    System.out.println(new String(decode(Base64.decode("lIBsfnzllmw9u/AGPJkf5qrRmxxU34GIDofHhD65S0EJDZAYIjAgiIdubKPkJvpD3GH+45cwHXaT/e51ee4lpX4Rw+d3ktmz"), "huodong12345")));

    System.out.println(Base64.encode(encode("jdbc:mysql://r_ibmp.dbop.com:3384/ibmp_q4?zeroDateTimeBehavior=convertToNull&autoReconnect=true".getBytes(), "huodong12345")));
    System.out.println(Base64.encode(encode("zz_user".getBytes(), "huodong12345")));
    System.out.println(Base64.encode(encode("zZ@ibmp_2014".getBytes(), "huodong12345")));

    System.out.println(new String(decode(Base64.decode("aX0jmfC9u3Fh/4U2JGTxM1KcLyJEPUFYbl4XuM0SyRU4YRvHcjnLwnP+0wQeIsnIdWtKg/bUd6FAVW0e+RIpn2JRNTXsnE7NAva32h/3JjeDL+dUi5OjIa57HVJwkuhmr/rmaz2x+B9XJ1aNfHAQgw=="), "11111111       aX0jmfC9u3Fh/4U2JGTxM1KcLyJEPUFYbl4XuM0SyRU4YRvHcjnLwnP+0wQeIsnIdWtKg/bUd6FAVW0e+RIpn2JRNTXsnE7NAva32h/3JjeDL+dUi5OjIa57HVJwkuhmr/rmaz2x+B9XJ1aNfHAQgw==")));

    System.out.println(new String(decode(Base64.decode("YLUxEe8MNWGijHbWqQHxPw=="), "huodong12345")));
  }
}